package com.nit.lab.service.basic.impl;

import com.nit.lab.dto.basic.StudentDTO;
import com.nit.lab.dto.utils.PagingDTO;
import com.nit.lab.dto.utils.TableReqDTO;
import com.nit.lab.dto.utils.TableRspDTO;
import com.nit.lab.entity.basic.StudentEntity;
import com.nit.lab.mapper.basic.StudentMapper;
import com.nit.lab.service.basic.StudentService;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.List;

/**
 * @文件名：StudentServiceImpl
 * @创建人: lxr
 * @创建时间: 2020/3/23
 * @描述:
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired(required = false)
    private StudentMapper studentMapper;

    @Value(value = "${application.jwt.hexKey}")
    private String hexKey;

    @Override
    public TableRspDTO list4Table(TableReqDTO tableReqDTO) throws Exception {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer add(StudentDTO studentDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        StudentEntity studentEntity = mapperFactory.getMapperFacade().map(studentDTO, StudentEntity.class);
        studentEntity.setStudentPwd(DigestUtils.md5DigestAsHex("lab205@nit".getBytes()));
        studentEntity.setGmtCreate(new Date());
        return studentMapper.add(studentEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addStudents(List<StudentDTO> studentDTOList) throws Exception {
        for (StudentDTO studentDTO : studentDTOList) {
            MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
            StudentEntity studentEntity = mapperFactory.getMapperFacade().map(studentDTO, StudentEntity.class);
            studentEntity.setStudentPwd(DigestUtils.md5DigestAsHex("lab205@nit".getBytes()));
            studentEntity.setGmtCreate(new Date());
            studentMapper.add(studentEntity);
        }
        return studentDTOList.size();
    }

    @Override
    public StudentDTO loginCheck(String studentCode) throws Exception {
        return studentMapper.getByLoginName(studentCode);
    }

    @Override
    public StudentDTO getByStudentId(Long studentId) throws Exception {
        return studentMapper.getByStudentId(studentId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer update(StudentDTO studentDTO) throws Exception {
        MapperFactory mapperFactory = new DefaultMapperFactory.Builder().build();
        StudentEntity studentEntity = mapperFactory.getMapperFacade().map(studentDTO, StudentEntity.class);
        return studentMapper.update(studentEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer remove(String studentIds) throws Exception {
        String[] arrayStudentId = studentIds.split(",");
        for (int i = 0; i < arrayStudentId.length; i++) {
            studentMapper.remove(Long.parseLong(arrayStudentId[i]));
        }
        return arrayStudentId.length;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer resetPwd(Long studentId, String studentPwd) throws Exception {
        return studentMapper.resetPwd(studentId, DigestUtils.md5DigestAsHex(studentPwd.getBytes()));
    }

}
